package com.myph.phctrl_t;

import com.example.phctrl_t.R;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.Toast;

public class SensorActivity extends Activity {

    Button btn_submit;
    Switch swi_on,swi_sleep_mode;
    RelativeLayout layout_on,layout_sleep_on;
    String ip, port;
    String on_or_off = "0",sleep_on_or_off = "0";
    StringBuilder response = new StringBuilder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        
        ip = Util.getSharedPref(this, "ip");
        port = Util.getSharedPref(this, "port");

        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });

        swi_on = (Switch) findViewById(R.id.swi_on);
        swi_on.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {
                    on_or_off = "1";
                    Toast.makeText(getApplicationContext(), "sensor ON",
                            Toast.LENGTH_SHORT).show();
                } else {
                    on_or_off = "0";
                    Toast.makeText(getApplicationContext(), "sensor OFF",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        layout_on = (RelativeLayout) findViewById(R.id.layout_on);
        layout_sleep_on = (RelativeLayout) findViewById(R.id.layout_sleep_on);
        layout_sleep_on.setVisibility(View.GONE);
        
        
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                response = new StringBuilder();
                String request = "$sensor," + on_or_off + "#";
                ClientSocketNew csn = new ClientSocketNew(ip, port, handler);
                csn.setNewRequest(request, response);
                csn.start();

                break;
            
            case 1002:
                Toast.makeText(getApplicationContext(), "fail to connect",
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            case 1003:
                if(response != null){
                    Log.d("response:",response.toString());
                    String resString = response.toString();
                    if (resString.equals("1")) {
                        Toast.makeText(getApplicationContext(), "成功!",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "失败!",
                                Toast.LENGTH_SHORT).show();
                    } 
                }else{
                    Toast.makeText(getApplicationContext(), "异常!",
                            Toast.LENGTH_SHORT).show();
                }
                finish();
                break;
            case 1004:
                Toast.makeText(getApplicationContext(), "操作失败", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                Toast.makeText(getApplicationContext(), "handler error!",
                        Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
}